/************************************************************
 * Project Name		[JavaEE_XML]
 * File Name     	[StudentUtil_Dom4j.java]
 * Creation Date 	[09-08-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_exam.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * Student Util implemented by dom4j
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class StudentUtil_Dom4j {

	private static final String fileName = "src/pkg_exam/Exam.xml";

	/**
	 * Load XML document
	 * 
	 * @return
	 * @throws DocumentException
	 */
	public static Document getDocument() throws DocumentException {
		SAXReader xmlParser = new SAXReader();
		Document document = xmlParser.read(fileName);
		return document;
	}

	/**
	 * Write to XML file
	 * 
	 * @param document
	 * @throws IOException
	 */
	public static void writeToXML(Document document) throws IOException {
		OutputStream out = new FileOutputStream(new File(fileName));
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("UTF-8");
		XMLWriter writer = new XMLWriter(out, format);
		writer.write(document);
		writer.flush();
		writer.close();
	}
}
